package com.example.demo.controller;


import com.example.demo.entity.ResponseInfo;
import com.example.demo.service.FileService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;

@RestController
@RequestMapping("/file")
@Api(tags = "文件管理")
@Slf4j
public class FileController {
    @Value("${filepath}")
    private String filepath;

    @Resource
    FileService fileService;

    /**
     * @param file 文件
     * @return 返回结果
     */
    @PostMapping("/upload")
    public ResponseInfo<String> fileUpload(@RequestParam("file") MultipartFile file) {
        File targetFile = new File(filepath);
        String filename = file.getOriginalFilename();
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }

        try {
            if (filename.substring(filename.lastIndexOf(".") + 1).equals("json") || filename.substring(filename.lastIndexOf(".") + 1).equals("har")) {
                File path = new File(filepath + filename);
                file.transferTo(path);
            } else {
                return ResponseInfo.errorInfo("请上传json格式或者har格式文件");
            }

        }
//        try (FileOutputStream out = new FileOutputStream(filepath + file.getOriginalFilename());) {
//            out.write(file.getBytes());
//            log.info("文件{}上传成功", file.getOriginalFilename());
//        }
        catch (Exception e) {
            e.printStackTrace();
            log.error("上传文件失败，原因={}", e.toString());
            return ResponseInfo.errorInfo(e.toString());
        }
        return ResponseInfo.successInfo(file.getOriginalFilename());
    }

    /**
     * @param fileName  文件名称
     * @param projectId 项目主键
     * @param url       需要全部替换的url
     * @param envId     需要全部替换的环境主键
     * @return 返回结果
     */
    @PostMapping("/fileAnalysis")
    public ResponseInfo<String> fileAnalysis(@RequestParam(value = "fileName") String fileName,
                                             @RequestParam(value = "projectId") int projectId,
                                             @RequestParam(value = "url") String url,
                                             @RequestParam(value = "envId") int envId,
                                             @RequestParam(value = "basePath",required = false) String basePath) {
        String result = fileService.AnalysisJson(filepath + fileName, projectId, url, envId,basePath);
        if (result.equals("导入成功")) {
            return ResponseInfo.successInfo("");
        } else {
            return ResponseInfo.errorInfo(result);
        }
    }
}
